//
//  MXYNavigationViewController.m
//  meiyinxiang
//
//  Created by apple on 2017/10/17.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MXYNavigationViewController.h"
#import "MYY_Me_Macro.h"
#import "MYY_LoginViewController.h"

@interface MXYNavigationViewController ()
{
    UINavigationController *_currentNavigationController;
}
@end

@implementation MXYNavigationViewController
+ (void)initialize
{
    if (self == [self class]) {
        UINavigationBar *bar = [UINavigationBar appearance];
        bar.barStyle = UIBarStyleDefault;
        bar.barTintColor = MYYheadColor;
        //[bar setBackgroundImage:[UIImage imageNamed:@"bg"] forBarMetrics:UIBarMetricsDefault];
        [bar setTranslucent:NO];
        [bar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:19]}];        
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(login) name:MYX_LoginOutNotificationName object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [self.navigationBar setBarStyle:UIBarStyleBlack];
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = self;
    }

}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 判断是否为栈底控制器
    if (self.viewControllers.count >0) {
        viewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController.navigationBar setBarTintColor:MYYheadColor];
        //设置导航子控制器按钮的加载样式
        UINavigationItem *vcBtnItem= [viewController navigationItem];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 40, 40);
        [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = (CGRect){CGPointZero, btn.currentImage.size};
        vcBtnItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    
    // if rootViewController, set delegate nil
    if (navigationController.viewControllers.count == 1) {
        navigationController.interactivePopGestureRecognizer.enabled = NO;
        navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}


-(UIViewController*)popViewControllerAnimated:(BOOL)animated
{
    return [super popViewControllerAnimated:animated];
}

- (void)back
{
    //    // 如果是忘记密码页，点击返回按钮直接跳转到登陆页【需要重构】
    //    if ([self.topViewController isKindOfClass:[JXTResetPasswordViewController class]]) {
    //        UIAlertView *alertView = [[UIAlertView alloc]
    //                                  initWithTitle:@"修改密码还没完成,\n确定退出吗?"
    //                                  message:nil
    //                                  delegate:self
    //                                  cancelButtonTitle:@"取消"
    //                                  otherButtonTitles:@"确定", nil];
    //
    //        [alertView show];
    //    }
    //    else {
    [self popViewControllerAnimated:YES];
    //    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
    }else if(buttonIndex == 1){
        [self popToRootViewControllerAnimated:YES];
    }
}


#pragma mark - login
- (void)login {
   // if (_currentNavigationController) {
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
    [SVProgressHUD showInfoWithStatus:@"用户未登录或超时登录，需要重新登录"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
        MYY_User *userd = [[MYY_User alloc]init];
        userd.token = @"";
        userd.isLogin = NO;
        userd.uid = @"";
        userd.accid = @"";
        userd.imtoken = @"";
        userd.account = @"";
        userd.password = @"";
        userd.avatar = @"";
        [userd save];
        if (userd.isPushToLoginPage) {
            
        } else {
            if (![_currentNavigationController.topViewController isKindOfClass:NSClassFromString(@"MYY_LoginViewController")]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"info" object:nil];
                MYY_LoginViewController *login = [[MYY_LoginViewController alloc] init];
                MXYNavigationViewController *nvc = [[MXYNavigationViewController alloc] initWithRootViewController:login];
                [self presentViewController:nvc animated:YES completion:nil];
            }
            userd.isPushToLoginPage = YES;
            [userd save];
            
        }
    });
    
    
    //}
}

#pragma mark - notification
-(void)removeNotification{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(login) name:MYX_LoginOutNotificationName object:nil];
    
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MYX_LoginOutNotificationName object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
